package com.ellricky.baselib.ui.cla

import android.view.View
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.util.StringUtils
import kotlinx.android.synthetic.main.fragment_class_experience.*
import kotlinx.android.synthetic.main.fragment_class_experience.btnSubmit
import kotlinx.android.synthetic.main.fragment_class_experience.etClass
import kotlinx.android.synthetic.main.fragment_class_experience.etClassEndTime
import kotlinx.android.synthetic.main.fragment_class_experience.etClassName
import kotlinx.android.synthetic.main.fragment_class_experience.etClassRoom
import kotlinx.android.synthetic.main.fragment_class_experience.etClassStartDate
import kotlinx.android.synthetic.main.fragment_class_experience.etClassStartTime
import kotlinx.android.synthetic.main.fragment_class_experience.label
import kotlinx.android.synthetic.main.fragment_official_class.*

class ExperienceClassFragment : BaseFragment<Void, BasePresenter<Void>>() {

    private var ePickerUtil: EPickerUtil? = null
    private var selectCourseWare: String = ""
    private var selectClassRoom: String = ""
    private var selectClassStartDate: String = ""
    private var selectClassStartTime: String = ""
    private var selectClassEndTime: String = ""
    private var className: String = ""
    private var classType: String = BaseConstant.ITEM_CLASS_A.toString()

    override fun getContentLayout(): Int {
        return R.layout.fragment_class_experience
    }

    override fun initUIView(p0: View?) {

        ePickerUtil = EPickerUtil(activity)

        label.setLabels(arrayListOf("2V6", "1V6"))
        label.setOnLabelSelectChangeListener { label,
                                               data,
                                               isSelect,
                                               position ->
            if (isSelect) {
                val dicName = data as String
                classType = if (dicName == "1V6") {
                    BaseConstant.ITEM_CLASS_B.toString()
                } else {
                    BaseConstant.ITEM_CLASS_A.toString()
                }
            }
        }

        etClass.setOnClickListener(this)
        etClassRoom.setOnClickListener(this)
        etClassStartDate.setOnClickListener(this)
        etClassStartTime.setOnClickListener(this)
        etClassEndTime.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            //课件
            R.id.etClass -> {

                ePickerUtil?.checkClassWarePicker(null) { _, t ->
                    selectCourseWare = t.id
                    etClass.value = t.name
                }
            }
            //教室列表
            R.id.etClassRoom -> {
                ePickerUtil?.checkClassRoomPicker(null) { _, t ->
                    selectClassRoom = t.id
                    etClassRoom.value = t.name
                }
            }
            //开课日期
            R.id.etClassStartDate -> {
                ePickerUtil?.showStartDatePicker("") { content,
                                                       _,
                                                       _ ->
                    selectClassStartDate = content
                    etClassStartDate.value = content
                }
            }
            //上课时间
            R.id.etClassStartTime -> {
                ePickerUtil?.showStartTimePicker("") { content,
                                                       _,
                                                       _ ->
                    selectClassStartTime = content
                    etClassStartTime.value = content
                }
            }
            //下课时间
            R.id.etClassEndTime -> {
                ePickerUtil?.showEndTimePicker("") { content,
                                                     _,
                                                     _ ->
                    selectClassEndTime = content
                    etClassEndTime.value = content
                }
            }
            //提交
            R.id.btnSubmit -> {
                className = etClassName.value
                if (className.isEmpty()) {
                    ToastUtil.show(context, "请填写班级名称")
                    return
                }
                createOfficialClass()
            }
        }
    }

    private fun createOfficialClass() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "gzaAddClassGrade",
            "eid" to LocalCacheUtil.getUid(),
            "class_name" to className,
            "class_type_dic_id" to "58",//（57正式班58体验班59临时班）
            "start_time" to selectClassStartTime,
            "end_time" to selectClassEndTime,
            "class_room_id" to selectClassRoom,
            "stage_id" to "",
            "course_type_id" to "",
            "start_time_id" to "",
            "createtime" to selectClassStartDate,
            "classify_id" to classType,
            "school_id" to LocalCacheUtil.getSchoolId(),
            "courseware_id" to selectCourseWare,
            "course_type" to "",
            "course_quantity" to ""
        )

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<Any>(mContext) {
                override fun onSuccess(data: Any, msg: String) {
                    ToastUtil.show(mContext, "创建成功")
                }
            })
    }


    companion object {
        fun newInstance(): ExperienceClassFragment {
            return ExperienceClassFragment()
        }
    }
}